package com.longrise.android.byjk.plugins.tabfirst;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;
import com.longrise.android.byjk.utils.Tracker_Google;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by callxkj on 2017/2/23.
 * 首页item中保险头条的这种adapter
 * recyclerview的图片底部有文字
 */

public class HomeRcvItem2Adapter extends BaseQuickAdapter<EntityBean, BaseViewHolder> {

    private List<EntityBean> mListBeans = new ArrayList<>();
    private OnItem2ClickListener mItem2ClickListener;

    public HomeRcvItem2Adapter(){
       super(R.layout.item_homercv2,null);
    }

    public void refreshData(EntityBean[] beans){
        mListBeans.clear();
        for (EntityBean bean : beans) {
            mListBeans.add(bean);
        }
        setNewData(mListBeans);
    }

    /**
     * 设置图片的点击事件
     */
    public void setOnItem2ClickListener(OnItem2ClickListener listener){
        this.mItem2ClickListener = listener;
    }

    @Override
    protected void convert(BaseViewHolder helper, EntityBean item) {
        final int position = helper.getLayoutPosition();
        View convertView = helper.getConvertView();
        TextView tv = (TextView) convertView.findViewById(R.id.item_homercv2_tv);
        ImageView iv = (ImageView) convertView.findViewById(R.id.item_homercv2_iv);

        EntityBean entityBean = mListBeans.get(position);
        String picpath = entityBean.getString("picpath");
        final String linkedurl = entityBean.getString("linkedurl");
        final String linkedtype = entityBean.getString("linkedtype");
        final String text = entityBean.getString("text");
        Glide.with(AppUtil.getContext()).load(picpath).placeholder(R.drawable.by_home_tuijian).into(iv);
        tv.setText(text);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItem2ClickListener!=null){
                    mItem2ClickListener.onClick(linkedurl,text,linkedtype);
                }
                addEvent(position);
            }
        });
    }


    private void addEvent(int position){
        if(position == 0){
            Tracker_Google.getInstance().addEvent("BB-headlines", "Recommended-news-one", "推荐咨询1");
        }else if(position == 1){
            Tracker_Google.getInstance().addEvent("BB-headlines", "Recommended-news-two", "推荐咨询2");
        }
    }


    interface OnItem2ClickListener{
        void onClick(String url,String title,String linkedtype);
    }
}
